#include <cmath>
#include <QDebug>
#include <QFontMetrics>
#include "element.h"

DrawRect::DrawRect(void)
    : m_start(0, 0),
      m_end(0, 0)
{
    m_drawType = RECTAGLE;
    m_state = Element::DS_IDLE;
}

DrawRect::~DrawRect(void)
{
}


int	DrawRect::mouseMove(QPoint point)
{
    if( m_state == Element::DS_STATE1 )
    {
        setTempEndPoint(point);
        return 1;
    }
    return 0;
}

int	DrawRect::mouseLButtonPress(QPoint point)
{
    if( m_state == Element::DS_IDLE )
    {
        setStartPoint(point);
        return 1;
    }
    return 0;
}
inline void DrawRect::setTempEndPoint(QPoint point)
{
    m_end = point;
    int x_min, y_min, x_max, y_max;
    x_min = qMin(m_start.x(), m_end.x());
    y_min = qMin(m_start.y(), m_end.y());
    x_max = qMax(m_start.x(), m_end.x());
    y_max = qMax(m_start.y(), m_end.y());
    m_rect.setTop(y_min);
    m_rect.setLeft(x_min);
    m_rect.setRight(x_max);
    m_rect.setBottom(y_max);
}
void DrawRect::build(QPoint start, QPoint end)
{
    setStartPoint(start);
    setEndPoint(end);
}

inline void DrawRect::setStartPoint(QPoint point)
{
    m_start = point;
    m_end = point;
    m_rect.setTopLeft( m_start );
    m_rect.setBottomRight( m_start );
    m_state = Element::DS_STATE1;
}

inline void DrawRect::setEndPoint(QPoint point)
{
    m_end = point;
    int x_min, y_min, x_max, y_max;
    x_min = qMin(m_start.x(), m_end.x());
    y_min = qMin(m_start.y(), m_end.y());
    x_max = qMax(m_start.x(), m_end.x());
    y_max = qMax(m_start.y(), m_end.y());
    m_rect.setTop(y_min);
    m_rect.setLeft(x_min);
    m_rect.setRight(x_max);
    m_rect.setBottom(y_max);

    m_area = m_rect.height() * m_rect.width();
    QPoint centre = (m_start + m_end) / 2;
    updateText();
    m_textBox.moveTo(centre);
    m_state = Element::DS_FINISH;
}

int DrawRect::mouseLButtonRelease(QPoint point)
{
    if( m_state == Element::DS_STATE1 )
    {
        setEndPoint(point);

        if(m_area < 30)
        {
            m_state = Element::DS_ERROR;
        }
        return 1;
    }
    return 0;
}

void DrawRect::draw( QPainter &painter )
{
    if( m_state != Element::DS_IDLE )
    {
        painter.save();
        painter.setPen(m_linePen);
        painter.drawRect ( m_rect );
        if( m_state == Element::DS_FINISH )
        {
            painter.setFont(m_textFont);
            painter.drawText(m_textBox, m_text);
        }
        painter.restore();
    }

}

void DrawRect::updateText(void)
{
    //m_text = QString(QStringLiteral("S = %1 um²")).arg(m_area * m_scale * m_scale, 2, 'f', 1);
    if(m_area * m_scale * m_scale >= 1e6)
    {
        m_text = QString::fromUtf8(u8"S = %1 mm²").arg(m_area * m_scale * m_scale / 1e6, 2, 'f', 2);
    }
    else
    {
        m_text = QString::fromUtf8(u8"S = %1 um²").arg(m_area * m_scale * m_scale, 2, 'f', 2);
    }
    QFontMetrics fm(m_textFont);
    int textWidth = fm.width(m_text);
    int textHeight = fm.height();
    m_textBox.setWidth(textWidth);
    m_textBox.setHeight(textHeight);
}

void DrawRect::computeBox()
{

}
